// pages/index/meituan/index.js
const app = getApp();
import Toast from "../../../vant/weapp/toast/toast";
Page({
  data: {
    headTop: "",
    headHeight: "",
    sumHeight: "",
    receipt_code: "",
    is_user_show: true,
    is_user: 0,
    showUser: false,
    room: "",
    index: 0,
    tab: 1,
    coupontype: 1,
    placeholder: "请输入卡券兑换"
  },

  onLoad(e) {

    console.log(wx.getStorageSync('latitude'))
    console.log(wx.getStorageSync('longitude'))
    let that = this;
    that.setData({
      tab:e.tab,
      headTop: app.globalData.headTop,
      headHeight: app.globalData.headHeight,
      sumHeight: app.globalData.sumHeight,
      receipt_code: '',
      is_user_show: true,
      is_user: 0,
      showUser: false,
    });
    that.getRoom()
  },
  chooseTab(e) {
    let placeholder = this.data.tab == 1 ? "请输入抖音卡券兑换" : "请输入美团卡券兑换"
    this.setData({
      tab: e.currentTarget.dataset.num,
      placeholder,
    })
  },
  coupontypeTab(e) {
    this.setData({
      coupontype: e.currentTarget.dataset.num,
    })
  },
  getRoom() {
    const that = this
    app.http.post(app.api.index, {
      lat: wx.getStorageSync('latitude') || 0,
      lng: wx.getStorageSync('longitude') || 0,
    }, (res) => {
      let store_id = res.data.list[that.data.index].room_id.toString()
      that.setData({
        room: res.data.list,
        store_id
      })

    })
  },
  //   登录
  login(e) {
    if (wx.getStorageSync('token') == "") {
      this.setData({
        is_user_show: false,
        is_user: 1
      });
    } else if (wx.getStorageSync('mobile') == "") {
      this.setData({
        is_user_show: false,
        is_user: 2
      });
    }
  },
  // 登录遮罩层
  close(e) {
    console.log(e)
    this.setData({
      is_user_show: e.detail,
      is_user: 0
    });
  },
  // 登陆后更新
  renew(e) {
    this.setData({
      showUser: true,
    });
    wx.setStorageSync('userInfo', e.detail)
    wx.setStorageSync('mobile', e.detail.mobile)
  },
  bindPickerChange(e) {
    const that = this
    that.setData({
      index: e.detail.value,
      store_id: that.data.room[e.detail.value].room_id
    })
  },
  code() {
    const that = this;
    if (wx.getStorageSync('mobile') == "") {
      that.login()
      return
    }
    wx.scanCode({
      success(res) {
        console.log(res)
        Toast.clear()
        that.setData({
          receipt_code: res.result
        })
      },
      fail(res) {
        Toast.fail('识别失败');
      }
    })
  },
  ljdh() {
    const that = this
    if (wx.getStorageSync('mobile') == "") {
      that.login()
      return
    }
    this.data.receipt_code=this.data.receipt_code.replace(/\s+/g, "");
    if (that.data.tab == 1) {
      that.meituan()
    } else if (that.data.tab == 2) {
      that.douyin()
    }
  },
  meituan() {
  
    app.http.post(app.api.Meituan, {
      receipt_code: this.data.receipt_code,
      coupontype: this.data.coupontype,
      room_id: this.data.store_id
    }, (res) => {
      if (res.code == 200) {
        wx.showModal({
          // title: '',
          content: res.message,
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              if(this.data.coupontype==1){
                wx.reLaunch({
                  url: '/pages/person/maincoupon/index',
                })
              }else{
                wx.reLaunch({
                  url: '/pages/person/mainpricecoupon/index',
                })

              }
           
            }
          }
        })
      } else {
        wx.showModal({
          // title: '',
          content: res.message,
          showCancel: false,
        })
      }

    })
  },
  douyin() {
    app.http.post(app.api.Douyin, {
      receipt_code: this.data.receipt_code,
      coupontype: this.data.coupontype,
      room_id: this.data.store_id
    }, (res) => {
      if (res.code == 200) {
        wx.showModal({
          // title: '',
          content: res.message,
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              if(this.data.coupontype==1){
                wx.reLaunch({
                  url: '/pages/person/maincoupon/index',
                })
              }else{
                wx.reLaunch({
                  url: '/pages/person/mainpricecoupon/index',
                })

              }
            }
          }
        })
      } else {
        wx.showModal({
          // title: '',
          content: res.message,
          showCancel: false,
        })
      }

    })
  },
  back() {
    wx.navigateBack({
      delta: 1,
    });
  },
});